<!DOCTYPE html>
<title>CSS Anchor Positioning Test: @position-try and cascade interaction</title>
<link rel="help" href="https://drafts.csswg.org/css-anchor-position-1/#fallback-apply">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
  .cb {
    position: relative;
    width: 100px;
    height: 100px;
    background: lightpink;
    display: inline-block;
  }
  .abs {
    position: absolute;
    background: darkcyan;
    left: 0px;
    top: 0px;
    width: 150px; /* force fallback */
    height: 25px;
    position-try-fallbacks: --pf;
  }
  @position-try --pf {
    width: 50px;
    left: 50px;
    top: 50px;
  }
</style>

<!-- Basic @position-try rule -->
<div class=cb>
  <div id=abs_try class=abs></div>
</div>
<script>
  test(() => {
    assert_equals(abs_try.offsetLeft, 50);
    assert_equals(abs_try.offsetTop, 50);
  }, '@position-try rule applies');
</script>

<!-- Inline style -->
<div class=cb>
  <div id=abs_inline class=abs style="left:20px"></div>
</div>
<script>
  test(() => {
    assert_equals(abs_inline.offsetLeft, 50);
    assert_equals(abs_inline.offsetTop, 50);
  }, '@position-try rule wins over inline style');
</script>

<!-- !important -->
<style>
  #abs_important {
    left: 10px !important;
  }
</style>
<div class=cb>
  <div id=abs_important class=abs></div>
</div>
<script>
  test(() => {
    assert_equals(abs_important.offsetLeft, 10);
    assert_equals(abs_important.offsetTop, 50);
  }, '@position-try rule does not win over !important');
</script>

<!-- Animations -->
<style>
  @keyframes anim {
    from { top: 20px; }
    to { top: 20px; }
  }
  #abs_animation {
    animation: anim 1000s steps(2, start) paused;
  }
</style>
<div class=cb>
  <div id=abs_animation class=abs></div>
</div>
<script>
  test(() => {
    assert_equals(abs_animation.offsetLeft, 50);
    assert_equals(abs_animation.offsetTop, 20);
  }, '@position-try rule does not win over animations');
</script>

<!-- Transitions -->
<style>
  #abs_transition.move {
    top: 10px !important;
    transition: top 1000s steps(2, start);
</style>
<div class=cb>
  <div id=abs_transition class=abs></div>
</div>
<script>
  test(() => {
    abs_transition.offsetTop;
    abs_transition.classList.add('move');
    assert_equals(abs_transition.offsetLeft, 50);
    assert_equals(abs_transition.offsetTop, 30);
  }, '@position-try rule does not win over transitions');
</script>

<!-- revert / revert-layer -->
<style>
  #abs_revert {
    position-try-fallbacks: --pf-revert;
  }
  @layer author-layer {
    #abs_revert {
      top: 30px;
      left: 30px;
    }
  }
  #abs_revert {
    top: 20px;
    left: 20px;
    /* overflowing .cb to force --pf-revert to be applied */
    width: 200px;
    height: 200px;
  }
  @position-try --pf-revert {
    left: revert;
    top: revert-layer;
    width: 30px;
    height: 30px;
  }
</style>
<div class=cb class=abs>
  <div id=abs_revert class=abs></div>
</div>
<script>
  test(() => {
    assert_equals(abs_revert.offsetLeft, 0, "left reverted back to user origin");
    assert_equals(abs_revert.offsetTop, 20, "top reverted back to author");
    assert_equals(abs_revert.offsetWidth, 30, "width from --pf-revert");
    assert_equals(abs_revert.offsetHeight, 30, "height from --pf-revert");
  }, '@position-try revert / revert-layer reverts to user / author origin');
</script>
